package com.yue.se.event.basic_event;

import java.util.HashSet;
import java.util.Set;

/**
 * 功能描述: TODO <br/>
 * 创建时间: 2016/7/29<br/>
 *
 * @author QiuYang
 */
public class YueEventSourceObject {
    private final static String DEFAULT_NAME = "defaultname";

    private String name;
    // 事件监听器容器
    private Set<YueEventListener> listeners;

    public YueEventSourceObject() {
        name = DEFAULT_NAME;
        listeners = new HashSet<>();
    }

    public YueEventSourceObject(String name, Set<YueEventListener> listeners) {
        this.name = name;
        this.listeners = listeners;
    }

    public YueEventSourceObject register(YueEventListener listener){
        listeners.add(listener);
        return this;
    }

    protected void notifies(){
        for (YueEventListener listener : listeners){
            listener.fireYueEvent(new YueEvent(this));
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (!this.name.equals(name)) {
            this.name = name;
            notifies();
        }
    }
}
